# -*- coding: utf-8 -*-

class AdminProductsController < AdminController
  def index
    @products = Product.order(:maker).all
  end
  
  def input_add
    @product = Product.new

    render :template => "/admin_products/input_update"
  end
  
  def input_edit
    @product = Product.find(params[:id])
    @keywords = Keyword.where(:product => params[:id]).all

    render :template => "/admin_products/input_update"
  end
  
  def update
    Product.transaction do
      product = params[:product][:id].blank? ? Product.new : Product.find(params[:product][:id])
      product.update_attributes(params[:product])
      product.save!
      product.id = Product.last.id if product.id <= 0
      
      Keyword.transaction do
         #キーワード
        keywords = Keyword.where(:product => product.id).all
        if not keywords.blank?
          keywords.each do |k|
            k.destroy
          end
        end
        params[:keyword].each do |k|
          next if k.blank?
          keyword = Keyword.new
          keyword.product = product.id
          keyword.keyword = k
          keyword.save!
        end
#render :text => params.inspect
#return

        Temperature.transaction do
           #温度
          temperatures = Temperature.where(:product => product.id).all
          if not temperatures.blank?
            temperatures.each do |t|
              t.destroy
            end
           end
          i = 1
          params[:temperature].each do |t|
            temperature = Temperature.new
            temperature.product = product.id
            temperature.temperature = i
            temperature.evaluation = t[1]
            temperature.save!
            i += 1
          end

          Fitscene.transaction do
             #合うシーン
            fitscenes = Fitscene.where(:product => product.id).all
            if not fitscenes.blank?
              fitscenes.each do |f|
                f.destroy
              end
            end
            i = 1
            if not params[:fitscene].blank?
              params[:fitscene].each do |f|
                fitscene = Fitscene.new
                fitscene.product = product.id
                fitscene.fitscene = f[0]
                fitscene.save!
                i += 1
              end
            end
            
            Impression.transaction do
              #見た目の印象
              impressions = Impression.where(:product => product.id).all
              if not impressions.blank?
                impressions.each do |i|
                  i.destroy
                end
              end
              i = 1
              if not params[:impression].blank?
                params[:impression].each do |imp|
                  impression = Impression.new
                  impression.product = product.id
                  impression.impression = imp[0]
                  impression.save!
                  i += 1
                end
              end
            end
          end
        end
      end
    end
    
    redirect_to :action => :index
  end
  
  def change_show
    product = Product.find(params[:id])
    product.is_show = product.is_show ? false : true
    product.save!
    
    redirect_to :action => :index
  end
end
